package com.yu.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.yu.config.result.BackResult;
import com.yu.config.result.BasePageResult;
import com.yu.domain.loginUser.dto.User;
import com.yu.domain.loginUser.param.UserParam;
import com.yu.domain.loginUser.vo.UserVo;
import com.yu.mapper.ds1.UserMapper;
import com.yu.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public BasePageResult pageList(UserParam param) {
        PageHelper.startPage(param.getPage(), param.getSize());
        List<User> users = userMapper.pageList(param);
        return BasePageResult.build(users);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BackResult addTransactional(UserVo param) {
        try {
            User user = new User();
            BeanUtils.copyProperties(param, user);
            userMapper.insert(user);
            QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
            queryWrapper.eq("id", "123653186415685");
            userMapper.delete(queryWrapper);
        }catch (Exception e) {
            e.printStackTrace();
            return BackResult.fail(e.getMessage());
        }

        return null;
    }
}
